import {Component, OnInit, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {MatchService} from "app/pages/match/match.service";

@Component({
  selector: 'ranking-detail',
  templateUrl: './ranking-detail.component.html',
  styleUrls: ['./ranking-detail.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class RankingDetailComponent implements OnInit {
  public rankingInfo; // 排名信息
  public userInfo;
  public dataStore = {
    matchType: 0, // 球类类型
    chartNo: 0, // 排名编号
    isTeam: 0,  // 是否团队
    rankingDetail: '' // 数据详情
  };
  public stateStore = {
    showAddress: false // 显示地址
  };
  public matchConfig;

  constructor(public route: ActivatedRoute,
              public service: MatchService,
              public router: Router) {
  }

  /**
   * @description 获取赛事排名详细信息
   * @author ouyqx-691
   * @date 2017/09/29
   */
  getRankingDetailByRankingId(params) {
    const param = {
      id: params['id'],
      chartNo: params['chartNo'],
      matchType: params['matchType']
    };
    this.service.getRankingDetail(param).subscribe(res => {
      if (res.result === '0' && res.data) {
        this.dataStore.chartNo = res.data.chartNo;
        const rankingInfo = res.data.basketballPage || res.data.footballPage || res.data.tennisPage ||
          res.data.badmintonPage || res.data.pingpongPage;

        if (rankingInfo) {
          this.rankingInfo = rankingInfo;
          this.getUserInfo(rankingInfo.userId);
        }
      }
    });
  }

  /**
   * @description 获取用户信息
   * @author ouyqx-691
   * @date 2017/10/11
   */
  getUserInfo(userId: string) {
    this.service.getUserInfo(userId).subscribe(res => {
      if (res.result === '0' && res.data) {
        this.userInfo = res.data;
        if (res.data.areaCityName) {
          this.stateStore.showAddress = true;
        }
      }
    });
  }

  /**
   * @description 跳转到个人主页
   * @author 李浩-1157
   * @date 2017/10/25
   */
  goToPosterPage() {
    if (this.userInfo.userId) {
      this.router.navigate([`/`, this.userInfo.userId]);
    }
  }

  ngOnInit() {
    this.route.queryParams.subscribe(params => {
      this.dataStore.matchType = Number.parseInt(params['matchType']);
      this.dataStore.isTeam = Number.parseInt(params['isTeam']);
      this.getRankingDetailByRankingId(params);
      this.matchConfig = this.service.getMatchConfig();

      // 获取需要展示的数据字段
      this.matchConfig.filter((value) => {
        value.typeList.filter((item) => {
          if (item.type === this.dataStore.matchType) {
            this.dataStore.rankingDetail = value.rankingDetail;
          }
        });
      });
    });
  }
}
